package huxibianjie.com.lbscandroid.ui.new_activity;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.widget.TextView;

import com.aspsine.swipetoloadlayout.OnLoadMoreListener;
import com.aspsine.swipetoloadlayout.OnRefreshListener;
import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;

import huxibianjie.com.lbscandroid.R;
import huxibianjie.com.lbscandroid.base.BaseActivity;
import huxibianjie.com.lbscandroid.manager.UserManager;
import huxibianjie.com.lbscandroid.model.SuperInviteRecordModel;
import huxibianjie.com.lbscandroid.model.UserModel;
import huxibianjie.com.lbscandroid.task.Callback;
import huxibianjie.com.lbscandroid.task.Task;
import huxibianjie.com.lbscandroid.ui.Service;
import huxibianjie.com.lbscandroid.ui.new_activity.adapter.InviteRecordAdapter;

/**
 * 邀请记录
 */
public class InviteRecordActivity extends BaseActivity {
    private TextView tvDoubleEnergy;

    private RecyclerView recyclerView;
    private SwipeToLoadLayout swipeLayout;

    private InviteRecordAdapter adapter;

    private int pageSize = 1;
    private int pageNumber = 10;

    @Override
    protected int getContentView() {
        return R.layout.activity_invite_record;
    }

    @Override
    protected void initView() {
        initTitle(R.drawable.ic_back, "邀请记录");

        tvDoubleEnergy = findViewById(R.id.tv_double_energy);
        swipeLayout = findViewById(R.id.swipeLayout);
        recyclerView = findViewById(R.id.swipe_target);
        LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setHasFixedSize(true);
        recyclerView.setItemAnimator(null);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter = new InviteRecordAdapter(this));
    }

    @Override
    protected void initData() {
        showLoading();
        requestData();
    }

    private void requestData() {
        if (!UserManager.getInstance().isLogin()) return;
        UserModel userModel = UserManager.getInstance().getUserModel();
        Task.create(Service.class).getInvitationRecord(String.valueOf(userModel.getId()), pageSize, pageNumber).enqueue(new Callback<SuperInviteRecordModel>() {
            @Override
            public void onSuccess(@Nullable SuperInviteRecordModel superInviteRecordModel) {
                cancelLoading();
                if (superInviteRecordModel == null || superInviteRecordModel.getInvitationRecordUser() == null)
                    return;
                if (pageSize == 1) {
                    swipeLayout.setRefreshing(false);
                    if (superInviteRecordModel.getInvitationRecordUser().size() == 0) {
                        adapter.clearAll();
                        swipeLayout.setLoadMoreEnabled(false);
                        return;
                    }
                    swipeLayout.setLoadMoreEnabled(!(superInviteRecordModel.getInvitationRecordUser().size() < 10));
                    adapter.replaceAll(superInviteRecordModel.getInvitationRecordUser());
                } else {
                    swipeLayout.setLoadingMore(false);
                    if (superInviteRecordModel.getInvitationRecordUser().size() == 0) {
                        swipeLayout.setLoadMoreEnabled(false);
                        showToast("没有更多了");
                    } else {
                        adapter.addAll(superInviteRecordModel.getInvitationRecordUser());
                    }
                }
                tvDoubleEnergy.setText(Html.fromHtml("共获得<font color='#13FF88'>" + superInviteRecordModel.getNumber() + "</font>双倍能量"));
            }

            @Override
            public void onFailure(@NonNull String error) {
                if (pageSize == 1) {
                    swipeLayout.setRefreshing(false);
                    swipeLayout.setLoadMoreEnabled(false);
                } else {
                    //数据获取失败 还原Page
                    pageSize--;
                    swipeLayout.setLoadingMore(false);
                }
                if (adapter.getItemCount() == 0) {
                }
                showToast(error);
                cancelLoading();
            }
        });
    }

    @Override
    protected void initListener() {
        swipeLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                pageSize = 1;
                requestData();
                swipeLayout.setRefreshing(false);
            }
        });
        swipeLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                pageSize++;
                requestData();
            }
        });
    }
}
